//Page Object
const app = getApp()
const util = require("../../../utils/util.js");
Page({
    data: {
        tAppid: app.globalData.tAppid,
        textraData: app.globalData.textraData,
        // cityname: app.globalData.extConfig.cityname,
        showLogin: false,
    },
    //options(Object)
    onLoad: function (options) {

        // 获取城市公众号二维码、分享图
        util.getUrl(app.globalData.url).then((res) => {
            util.cityConfig().then(res => {
                this.setData({
                    qrcodeimg: res.qrcode ? res.qrcode : '',
                    shareimg: res.shareimg ? res.shareimg : '',
                    city: res.cityname ? res.cityname : ks.getStorageSync('city'),
                    citycode: res.citycode ? res.citycode : ks.getStorageSync('citycode'),
                    wxurl: "http://www.bendibao.com/app/m.html"
                })

            })
        })
    },

    focusBtn() {
        if (this.data.wxurl != '') {
            ks.navigateTo({
                url: '/pages/webview/webview?path=' + encodeURIComponent(this.data.wxurl),
            })
        } else if (this.data.qrcodeimg != '' && this.data.wxurl == '') {
            ks.navigateTo({
                url: '/pages/mine/follow/follow',
            })
        } else {
            ks.showModal({
                title: '温馨提醒',
                content: '暂未开通本地订阅号，敬请期待哦~ 是否现在返回首页继续浏览本地资讯？',
                success: res => {
                    if (res.confirm) {
                        ks.switchTab({
                            url: '/pages/index/index',
                        })
                    }
                }
            })
        }
    },

    // 获取用户授权并收藏
    getUserInfo(e) {
        if (!this.data.showLogin) {
            ks.getUserInfo({
                withCredentials: true,
                success: res => {
                    ks.setStorageSync("userInfo", res.userInfo)
                    this.setData({
                        userInfo: res.userInfo,
                    })
                    let loginData = {
                        action: "ks_userInfo",
                        session_key: ks.getStorageSync("session_key"),
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        openid: ks.getStorageSync('openid') || '',
                        // unionid: ks.getStorageSync('unionid') || ''
                    };
                    util.request('user.php', loginData, 'POST').then(res => {
                        if (res.data.state == 1) {
                            ks.setStorageSync("user_id", res.data.data.user_id)
                            this.setData({
                                showLogin: true,
                                user_id: ks.getStorageSync("user_id")
                            })
                        } else
                            ks.showToast({
                                title: '登录失败，请检查快手是否更新到最新版',
                                icon: 'none',
                            });
                    })
                },
                fail: () => {
                    ks.showToast({
                        title: '请授权后再登录',
                        icon: 'none',
                    });
                }
            })
        }
    },

    collectClick(e) {
        let type = e.currentTarget.dataset.type;
        if (ks.getStorageSync("user_id")) {
            if (type)
                ks.navigateTo({
                    url: '/pages/mine/zuji/zuji',
                })
            else
                ks.navigateTo({
                    url: '/pages/mine/collect/index/index',
                })
        } else {
            ks.showToast({
                icon: "none",
                title: '请先登录哦~',
            })
        }
    },

    feedback(){
        ks.showModal({
            title: '',
            content: '请点击右上角的 ...中底部的“反馈”',
            showCancel: false,
            success(res) {
            }
        })
    },

    // 退出登录
    delogin() {
        ks.removeStorage({
            key: 'user_id',
            success: (res) => {
                this.setData({
                    showLogin: false,
                })
            }
        })

    },

    onShow() {
        if (ks.getStorageSync("user_id")) {
            this.setData({
                showLogin: true,
            })
        }

        if (ks.getStorageSync("userInfo")) {
            this.setData({
                userInfo: ks.getStorageSync("userInfo"),
            })
        }
    },

    onShareAppMessage() {
        return {
            title: "本地宝",
            path: "pages/index/index",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
        }
    }

});